package com.sgb.zhaocai.view.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.zhaocai.R;
import com.sgb.zhaocai.databinding.FragmentWebsiteInvitedListBinding;
import com.sgb.zhaocai.entity.AddInviteExternalListBean;
import com.sgb.zhaocai.entity.RequestBean;
import com.sgb.zhaocai.entity.TenderExternalListEntity;
import com.sgb.zhaocai.view.activity.PickInvitedApplyDetailsActivity;
import com.sgb.zhaocai.view.adapter.WebSiteInvitedAdapter;
import com.sgb.zhaocai.viewmodel.WebSiteInvitedListModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.xrecyclerview.XRecyclerView;
import java.util.ArrayList;
import java.util.List;

/**
 * Time:2021/12/17
 * AUTHOR:F_Q
 * Description:
 */
public class WebSiteInvitedListFragment extends AppLazyFragment {

    private FragmentWebsiteInvitedListBinding binding;
    private WebSiteInvitedListModel mModel;
    RequestBean requestbean = new RequestBean();
    //1：邀请报名   2：邀请竞价
    private String fromWhere = "";
    private String SearchName = "";
    private boolean isLoadMore = false;
    private WebSiteInvitedAdapter websiteinvitedadapter;
    AddInviteExternalListBean DataBean = new AddInviteExternalListBean();
    List<AddInviteExternalListBean.AddInviteDataBean> lsibean = new ArrayList<>();

    public static WebSiteInvitedListFragment newInstance(String fromWhere) {
        WebSiteInvitedListFragment fragment = new WebSiteInvitedListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("fromWhere", fromWhere);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_website_invited_list, container, false);
        binding.setLifecycleOwner(this);
        mModel = new ViewModelProvider(this).get(WebSiteInvitedListModel.class);
        mModel.setActivity(WebSiteInvitedListFragment.this);
        fromWhere = getArguments().getString("fromWhere");
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        super.initView();
        websiteinvitedadapter = new WebSiteInvitedAdapter(getActivity(), null, fromWhere);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.recyclerView.setAdapter(websiteinvitedadapter);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initData();
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                requestbean.page++;
                //上拉加载更多
                getonLoadMore();
            }
        });

        websiteinvitedadapter.setOnItemStateOperate(new WebSiteInvitedAdapter.OnItemStateOperate() {
            @Override
            public void OnAgainInviteData(TenderExternalListEntity tenderExternalListEntity) {
                AddInviteExternalListBean.AddInviteDataBean  bean =new AddInviteExternalListBean.AddInviteDataBean();
                bean.setContacts(tenderExternalListEntity.getContacts());
                bean.setTelephone(tenderExternalListEntity.getTelephone());
                bean.setCompanyName(tenderExternalListEntity.getCompanyName());
                lsibean.add(bean);
                DataBean.setAddInviteExternalDTOList(lsibean);
                //再次邀请实现
                switch (fromWhere) {
                    case "1":
                        DataBean.setBusinessId(tenderExternalListEntity.getTenderingId());
                        PickInvitedApplyDetailsActivity.start(getActivity(), "3", tenderExternalListEntity.getTenderName(), tenderExternalListEntity.getCreated()
                                , 1, new ArrayList<>(), tenderExternalListEntity.getTenderingId(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), 0, "",DataBean);
                        break;
                    case "2":
                        DataBean.setBusinessId(tenderExternalListEntity.getBidId());
                        PickInvitedApplyDetailsActivity.start(getActivity(), "4", tenderExternalListEntity.getBidName(), ""
                                , 1, new ArrayList<>(), tenderExternalListEntity.getBidId(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), 0, "",DataBean);
                        break;
                }

            }
        });

    }

    private void getonLoadMore() {
        switch (fromWhere) {
            case "1"://邀请报名
                mModel.getWebSiteInvitedList(requestbean);
                break;
            case "2"://邀请竞价
                mModel.getWebSiteBiddingList(requestbean);
                break;
        }
    }

    @Override
    protected void onLazyLoad() {
        initData();
    }

    private void initData() {
        isLoadMore = false;
        requestbean.page = 1;
        requestbean.setKeyWord(SearchName);
        requestbean.setBusinessId("");
        requestbean.setTotal(0);
        switch (fromWhere) {
            case "1"://邀请报名
                mModel.getWebSiteInvitedList(requestbean);
                break;
            case "2"://邀请竞价
                mModel.getWebSiteBiddingList(requestbean);
                break;
        }

    }

    public void updateData(String searchName) {
        if (TextUtils.isEmpty(searchName)) {
            SearchName = "";
        } else {
            SearchName = searchName;
        }
        initData();
    }

    /**
     * 邀请报名数据展示
     */
    public void setWebSiteInvitedList(List<TenderExternalListEntity> data, boolean isLastPage) {
        RecyclerRefreshing();
        if (isLoadMore) {
            if (data != null && data.size() > 0) {
                websiteinvitedadapter.addDatas(data);
            }
            binding.recyclerView.setNoMore(isLastPage);
        } else {
            if (data != null && data.size() > 0) {
                binding.empty.setVisibility(View.GONE);
                binding.recyclerView.setVisibility(View.VISIBLE);
                websiteinvitedadapter.setDatas(data);
                binding.recyclerView.scrollToPosition(0);
                binding.recyclerView.setNoMore(isLastPage);
            } else {
                websiteinvitedadapter.clearDatas();
                binding.empty.setVisibility(View.VISIBLE);
                binding.recyclerView.setVisibility(View.GONE);
            }
        }
    }


    private void RecyclerRefreshing() {
        binding.recyclerView.refreshComplete();
        binding.recyclerView.loadMoreComplete();
    }

    /**
     * 邀请竞价数据展示
     */
    public void setWebSiteBiddingList(List<TenderExternalListEntity> data, boolean isLastPage) {
        RecyclerRefreshing();
        if (isLoadMore) {
            if (data != null && data.size() > 0) {
                websiteinvitedadapter.addDatas(data);
            }
            binding.recyclerView.setNoMore(isLastPage);
        } else {
            if (data != null && data.size() > 0) {
                binding.empty.setVisibility(View.GONE);
                binding.recyclerView.setVisibility(View.VISIBLE);
                websiteinvitedadapter.setDatas(data);
                binding.recyclerView.scrollToPosition(0);
                binding.recyclerView.setNoMore(isLastPage);
            } else {
                websiteinvitedadapter.clearDatas();
                binding.empty.setVisibility(View.VISIBLE);
                binding.recyclerView.setVisibility(View.GONE);
            }
        }
    }
}
